/**
 * callbacks in c++
 * - lambda mode
 * ref: http://www.tedfelix.com/software/c++-callbacks.html
 */
#include <functional>
#include <iostream>

class Callee {
public:
    Callee(int i)
        : m_i(i) {};

    int callback_function(int i)
    {
        std::cout << " Callee::callback_function inside callback\n"
                  << std::endl;
        return m_i * i;
    }

private:
    int m_i;
};

typedef std::function<int(int)> CallbackFunction;

class Caller {
public:
    void connect_callback(CallbackFunction cb)
    {
        m_cb = cb;
    }

    void test()
    {
        std::cout << "Caller::test() calling callback...\n"
                  << std::endl;

        int i = m_cb(10);
        std::cout << "Result (100): " << i << std::endl;
    }

private:
    CallbackFunction m_cb;
};

int main(int argc, char* argv[])
{
    Caller caller;
    Callee callee(10);

    caller.connect_callback([&callee](int i) { return callee.callback_function(i); });

    caller.test();

    return 0;
}
